<?php

namespace Zms\Service;

class System
{
    /**
     * 运行环境
     * @return array{type:string,name:string,os:string}
     */
    public static function environment(): array
    {
        $os = strtolower(PHP_OS);
        if ($os === 'linux') {
            //wsl下的linux环境
            if (isset($_SERVER['WSL_DISTRO_NAME'])) {
                return ['type' => 'wsl', 'name' => "[WSL]{$_SERVER['WSL_DISTRO_NAME']}", 'os' => 'linux'];
            }
            //docker中的Linux环境
            $scanned = strtolower(php_ini_scanned_files() ?: '');
            if ($scanned && preg_match('/(docker)/', $scanned)) {
                return ['type' => 'docker', 'name' => php_uname('r'), 'os' => 'linux'];
            }
            $bin = $_SERVER['_'] ?? '';
            if ($bin && preg_match('/(php)/', $bin)) {
                return ['type' => 'linux', 'name' => php_uname('r'), 'os' => 'linux php'];
            }
            //原生linux环境
            return ['type' => 'linux', 'name' => php_uname('r'), 'os' => 'linux swoole-cli'];
        }
        //windows+swoole-cli下的环境
        if (preg_match('/(cygwin)/', $os)) {
            return ['type' => 'cygwin', 'name' => "windows", 'os' => 'windows'];
        }
        //未知环境
        return ['type' => 'unknown', 'name' => "unknown", 'os' => $os];
    }
}